package com.tiger.userClient.http.banner;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiger.cache.RedisService;
import com.tiger.db.entity.Banner;
import com.tiger.userClient.Db;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.util.concurrent.TimeUnit.SECONDS;

@Service
public class BannerService {
    private static String key = "banner";
    private final Db db;
    private final RedisService redisService;

    public BannerService(Db db, RedisService redisService) {
        this.db = db;
        this.redisService = redisService;
    }

    public Object list() {
        return db.banner.list(new LambdaQueryWrapper<Banner>().select(Banner::getName, Banner::getCover, Banner::getLink).eq(Banner::isEnable, true));
//        String res = redisService.template.opsForValue().get(key);
//        if (res != null && !res.isEmpty()) {
//
//            JSONObject jsonObject = JSON.parseObject(res);
//
//            boolean lock = Boolean.TRUE.equals(redisService.template.opsForValue().setIfAbsent("get:banner", "1", 10, SECONDS));
//            if(lock) {
//                new Thread(() -> {
//                    List<Banner> banners = db.banner.list(new LambdaQueryWrapper<Banner>().select(Banner::getName, Banner::getCover, Banner::getLink).eq(Banner::isEnable, true));
//                    if(!banners.isEmpty()) {
//                        redisService.template.opsForValue().set(key, JSON.toJSONString(banners));
//                    }
//                }).start();
//            }
//
//
//            return jsonObject;
//
//
//        }else {
//            List<Banner> banners = db.banner.list(new LambdaQueryWrapper<Banner>().select(Banner::getName, Banner::getCover, Banner::getLink).eq(Banner::isEnable, true));
//
//            if(!banners.isEmpty()) {
//                boolean lock = Boolean.TRUE.equals(redisService.template.opsForValue().setIfAbsent("set:banner", "1", 10, SECONDS));
//                if(lock) {
//                    redisService.template.opsForValue().set(key, JSON.toJSONString(banners));
//                }
//            }
//
//            return banners;
//        }

    }



}
